package com.mmall.mmall002.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mmall.mmall002.entity.*;
import com.mmall.mmall002.mapper.*;
import com.mmall.mmall002.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mmall.mmall002.vo.OrderDetailVO;
import com.mmall.mmall002.vo.OrderVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 龙菲
 * @since 2021-03-02
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private ProductMapper productMapper;

    @Override
    public boolean save(Orders o, User user,String address,String remark) {

        //判断是新地址还是老地址
        if(o.getUserAddress().equals("newAddress")){
            //存入数据库
            UserAddress userAddress  = new UserAddress();
            userAddress.setAddress(address);
            userAddress.setRemark(remark);
            userAddress.setIsdefault(1);
            userAddress.setUserId(user.getId());



            //修改就地址的默认选择为0
            QueryWrapper qw = new QueryWrapper();
            qw.eq("isdefault",1);
            qw.eq("user_id",user.getId());
            UserAddress old = userAddressMapper.selectOne(qw);
            old.setIsdefault(0);
            userAddressMapper.updateById(old);

            //添加新地址  并且设置默认为1
            userAddressMapper.insert(userAddress);

            o.setUserAddress(address);

        }

        /**
         *  存储orders
         */
        o.setUserId(user.getId());
        o.setLoginName(user.getLoginName());

        String seriaNumber = null;
        try {
            StringBuffer result = new StringBuffer();
            for(int i=0;i<32;i++) {
                result.append(Integer.toHexString(new Random().nextInt(16)));
            }
            seriaNumber =  result.toString().toUpperCase();
        } catch (Exception e) {
            e.printStackTrace();
        }

        o.setSerialnumber(seriaNumber);


        /**
         * 存储OrdersDetails
         */
        QueryWrapper qw = new QueryWrapper();
        qw.eq("user_id",user.getId());
        //根据用户编号查询购物车信息
        List<Cart> cartList = cartMapper.selectList(qw);

        List<OrderDetail> details  = new ArrayList<>();

        //存储新的订单
        orderMapper.insert(o);

        for (Cart cart : cartList) {
            OrderDetail orderDetail = new OrderDetail();
            BeanUtils.copyProperties(cart,orderDetail);
            orderDetail.setId(null);
            orderDetail.setOrderId(o.getId());

            orderDetailMapper.insert(orderDetail);
        }


        //清空购物车
        //根据用户编号清除
        qw = new QueryWrapper();
        qw.eq("user_id",user.getId());

        cartMapper.delete(qw);

        return true;
    }

    /**
     * 根据用户编号查询所有订单信息
     */
    @Override
    public List<OrderVO> findAllOrdersByUserId(User user) {
        //根据用户编号查询所有订单信息集合
        QueryWrapper qw = new QueryWrapper();
        qw.eq("user_id",14);
        List<Orders> ordersList = orderMapper.selectList(qw);
        List<OrderVO> orderVOList = new ArrayList<>();



        for (Orders orders : ordersList) {
            OrderVO orderVO = new OrderVO();
            BeanUtils.copyProperties(orders,orderVO);
            //根据订单编号查询出订单详情
            qw = new QueryWrapper();
            qw.eq("order_id",orders.getId());

            List<OrderDetail> orderDetailList = orderDetailMapper.selectList(qw);
            List<OrderDetailVO> orderDetailVOList = new ArrayList<>();

            for (OrderDetail orderDetail : orderDetailList) {
                OrderDetailVO orderDetailVO = new OrderDetailVO();
                BeanUtils.copyProperties(orderDetail,orderDetailVO);

                //根据订单详情总的商品编号查询商品信息
                Product product = productMapper.selectById(orderDetail.getProductId());
                BeanUtils.copyProperties(product,orderDetailVO);

                orderDetailVOList.add(orderDetailVO);
            }
            orderVO.setDetailList(orderDetailVOList);
            orderVOList.add(orderVO);
        }

        return orderVOList;
    }
}
